home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / demo / wemdemo4.zip / INFO / LEMACS.5 (.txt) < prev    next >
GNU Info File  |  1994-09-21  |  47KB  |  804 lines

  1. This is Info file ../info/lemacs, produced by Makeinfo-1.55 from the
  2. input file lemacs.txi.
  3.    This file documents the GNU Emacs editor.
  4.    Copyright (C) 1985, 1986, 1988 Richard M. Stallman.  Copyright (C)
  5. 1991, 1992 Lucid, Inc.
  6.    Permission is granted to make and distribute verbatim copies of this
  7. manual provided the copyright notice and this permission notice are
  8. preserved on all copies.
  9.    Permission is granted to copy and distribute modified versions of
  10. this manual under the conditions for verbatim copying, provided also
  11. that the sections entitled "The GNU Manifesto", "Distribution" and "GNU
  12. General Public License" are included exactly as in the original, and
  13. provided that the entire resulting derived work is distributed under the
  14. terms of a permission notice identical to this one.
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions, except that the sections entitled "The GNU Manifesto",
  18. "Distribution" and "GNU General Public License" may be included in a
  19. translation approved by the author instead of in the original English.
  20. File: lemacs,  Node: Incremental Search,  Next: Non-Incremental Search,  Prev: Search,  Up: Search
  21. Incremental Search
  22. ==================
  23.    An incremental search begins searching as soon as you type the first
  24. character of the search string.  As you type in the search string, Emacs
  25. shows you where the string (as you have typed it so far) is found.
  26. When you have typed enough characters to identify the place you want,
  27. you can stop.  Depending on what you do next, you may or may not need to
  28. terminate the search explicitly with a RET.
  29. `C-s'
  30.      Incremental search forward (`isearch-forward').
  31. `C-r'
  32.      Incremental search backward (`isearch-backward').
  33.    `C-s' starts an incremental search.  `C-s' reads characters from the
  34. keyboard and positions the cursor at the first occurrence of the
  35. characters that you have typed.  If you type `C-s' and then `F', the
  36. cursor moves right after the first `F'.  Type an `O', and see the
  37. cursor move to after the first `FO'.  After another `O', the cursor is
  38. after the first `FOO' after the place where you started the search.
  39. Meanwhile, the search string `FOO' has been echoed in the echo area.
  40.    The echo area display ends with three dots when actual searching is
  41. going on.  When search is waiting for more input, the three dots are
  42. removed.  (On slow terminals, the three dots are not displayed.)
  43.    If you make a mistake in typing the search string, you can erase
  44. characters with DEL.  Each DEL cancels the last character of the search
  45. string.  This does not happen until Emacs is ready to read another
  46. input character; first it must either find, or fail to find, the
  47. character you want to erase.  If you do not want to wait for this to
  48. happen, use `C-g' as described below.
  49.    When you are satisfied with the place you have reached, you can type
  50. RET (or C-m), which stops searching, leaving the cursor where the
  51. search brought it.  Any command not specially meaningful in searches
  52. also stops the search and is then executed.  Thus, typing `C-a' exits
  53. the search and then moves to the beginning of the line.  RET is
  54. necessary only if the next command you want to type is a printing
  55. character, DEL, ESC, or another control character that is special
  56. within searches (`C-q', `C-w', `C-r', `C-s' or `C-y').
  57.    Sometimes you search for `FOO' and find it, but were actually
  58. looking for a different occurance of it.  To move to the next occurrence
  59. of the search string, type another `C-s'.  Do this as often as
  60. necessary.  If you overshoot, you can cancel some `C-s' characters with
  61.    After you exit a search, you can search for the same string again by
  62. typing just `C-s C-s': the first `C-s' is the key that invokes
  63. incremental search, and the second `C-s' means "search again".
  64.    If the specified string is not found at all, the echo area displays
  65. the text `Failing I-Search'.  The cursor is after the place where Emacs
  66. found as much of your string as it could.  Thus, if you search for
  67. `FOOT', and there is no `FOOT', the cursor may be after the `FOO' in
  68. `FOOL'.  At this point there are several things you can do.  If you
  69. mistyped the search string, correct it.  If you like the place you have
  70. found, you can type RET or some other Emacs command to "accept what the
  71. search offered".  Or you can type `C-g', which removes from the search
  72. string the characters that could not be found (the `T' in `FOOT'),
  73. leaving those that were found (the `FOO' in `FOOT').  A second `C-g' at
  74. that point cancels the search entirely, returning point to where it was
  75. when the search started.
  76.    If a search is failing and you ask to repeat it by typing another
  77. `C-s', it starts again from the beginning of the buffer.  Repeating a
  78. failing reverse search with `C-r' starts again from the end.  This is
  79. called "wrapping around".  `Wrapped' appears in the search prompt once
  80. this has happened.
  81.    The `C-g' "quit" character does special things during searches; just
  82. what it does depends on the status of the search.  If the search has
  83. found what you specified and is waiting for input, `C-g' cancels the
  84. entire search.  The cursor moves back to where you started the search.
  85. If `C-g' is typed when there are characters in the search string that
  86. have not been found--because Emacs is still searching for them, or
  87. because it has failed to find them--then the search string characters
  88. which have not been found are discarded from the search string.  The
  89. search is now successful and waiting for more input, so a second `C-g'
  90. cancels the entire search.
  91.    To search for a control character such as `C-s' or DEL or ESC, you
  92. must quote it by typing `C-q' first.  This function of `C-q' is
  93. analogous to its meaning as an Emacs command: it causes the following
  94. character to be treated the way a graphic character would normally be
  95. treated in the same context.
  96.    To search backwards, you can use `C-r' instead of `C-s' to start the
  97. search; `C-r' is the key that runs the command (`isearch-backward') to
  98. search backward.  You can also use `C-r' to change from searching
  99. forward to searching backwards.  Do this if a search fails because the
  100. place you started was too far down in the file.  Repeated `C-r' keeps
  101. looking for more occurrences backwards.  `C-s' starts going forward
  102. again.  You can cancel `C-r' in a search with DEL.
  103.    The characters `C-y' and `C-w' can be used in incremental search to
  104. grab text from the buffer into the search string.  This makes it
  105. convenient to search for another occurrence of text at point.  `C-w'
  106. copies the word after point as part of the search string, advancing
  107. point over that word.  Another `C-s' to repeat the search will then
  108. search for a string including that word.  `C-y' is similar to `C-w' but
  109. copies the rest of the current line into the search string.
  110.    The characters `M-p' and `M-n' can be used in an incremental search
  111. to recall things which you have searched for in the past.  A list of
  112. the last 16 things you have searched for is retained, and `M-p' and
  113. `M-n' let you cycle through that ring.
  114.    The character `M-TAB' does completion on the elements in the search
  115. history ring.  For example, if you know that you have recently searched
  116. for the string `POTATOE', you could type `C-s P O M-TAB'.  If you had
  117. searched for other strings beginning with `PO' then you would be shown
  118. a list of them, and would need to type more to select one.
  119.    You can change any of the special characters in incremental search
  120. via the normal keybinding mechanism: simply add a binding to the
  121. `isearch-mode-map'.  For example, to make the character `C-b' mean
  122. "search backwards" while in isearch-mode, do this:
  123.      (define-key isearch-mode-map "\C-b" 'isearch-repeat-backward)
  124.    These are the default bindings of isearch-mode:
  125. `DEL'
  126.      (`isearch-delete-char')  Delete a character from the incremental
  127.      search string.
  128. `RET'
  129.      (`isearch-exit')  Exit incremental search.
  130. `C-q'
  131.      (`isearch-quote-char')  Quote special characters for incremental
  132.      search.
  133. `C-s'
  134.      (`isearch-repeat-forward')  Repeat incremental search forward.
  135. `C-r'
  136.      (`isearch-repeat-reverse')  Repeat incremental search backward.
  137. `C-y'
  138.      (`isearch-yank-line')  Pull rest of line from buffer into search
  139.      string.
  140. `C-w'
  141.      (`isearch-yank-word')  Pull next word from buffer into search
  142.      string.
  143. `C-g'
  144.      (`isearch-abort')  Cancels input back to what has been found
  145.      successfully, or aborts the isearch.
  146. `M-p'
  147.      (`isearch-ring-retreat')  Recall the previous element in the
  148.      isearch history ring.
  149. `M-n'
  150.      (`isearch-ring-advance')  Recall the next element in the isearch
  151.      history ring.
  152. `M-TAB'
  153.      (`isearch-complete')  Do completion on the elements in the isearch
  154.      history ring.
  155.    Any other character which is normally inserted into a buffer when
  156. typed is automatically added to the search string in isearch-mode.
  157. Slow Terminal Incremental Search
  158. --------------------------------
  159.    Incremental search on a slow terminal uses a modified style of
  160. display that is designed to take less time.  Instead of redisplaying
  161. the buffer at each place the search gets to, it creates a new
  162. single-line window and uses that to display the line the search has
  163. found.  The single-line window appears as soon as point gets outside of
  164. the text that is already on the screen.
  165.    When the search is terminated, the single-line window is removed.
  166. Only at this time the window in which the search was done is
  167. redisplayed to show its new value of point.
  168.    The three dots at the end of the search string, normally used to
  169. indicate that searching is going on, are not displayed in slow style
  170. display.
  171.    The slow terminal style of display is used when the terminal baud
  172. rate is less than or equal to the value of the variable
  173. `search-slow-speed', initially 1200.
  174.    The number of lines to use in slow terminal search display is
  175. controlled by the variable `search-slow-window-lines'.  Its normal
  176. value is 1.
  177. File: lemacs,  Node: Non-Incremental Search,  Next: Word Search,  Prev: Incremental Search,  Up: Search
  178. Non-Incremental Search
  179. ======================
  180.    Emacs also has conventional non-incremental search commands, which
  181. require you type the entire search string before searching begins.
  182. `C-s RET STRING RET'
  183.      Search for STRING.
  184. `C-r RET STRING RET'
  185.      Search backward for STRING.
  186.    To do a non-incremental search, first type `C-s RET' (or `C-s C-m'.
  187. This enters the minibuffer to read the search string.  Terminate the
  188. string with RET to start the search.  If the string is not found the
  189. search command gets an error.
  190.    By default, `C-s' invokes incremental search, but if you give it an
  191. empty argument, which would otherwise be useless, it invokes
  192. non-incremental search.  Therefore, `C-s RET' invokes non-incremental
  193. search.  `C-r RET' also works this way.
  194.    Forward and backward non-incremental searches are implemented by the
  195. commands `search-forward' and `search-backward'.  You can bind these
  196. commands to keys.  The reason that incremental search is programmed to
  197. invoke them as well is that `C-s RET' is the traditional sequence of
  198. characters used in Emacs to invoke non-incremental search.
  199.    Non-Incremental searches performed using `C-s RET' do not call
  200. `search-forward' right away.  They first check if the next character is
  201. `C-w', which requests a word search.
  202.    *Note Word Search::.
  203. File: lemacs,  Node: Word Search,  Next: Regexp Search,  Prev: Non-Incremental Search,  Up: Search
  204. Word Search
  205. ===========
  206.    Word search looks for a sequence of words without regard to how the
  207. words are separated.  More precisely, you type a string of many words,
  208. using single spaces to separate them, and the string is found even if
  209. there are multiple spaces, newlines or other punctuation between the
  210. words.
  211.    Word search is useful in editing documents formatted by text
  212. formatters.  If you edit while looking at the printed, formatted
  213. version, you can't tell where the line breaks are in the source file.
  214. Word search, allows you to search  without having to know the line
  215. breaks.
  216. `C-s RET C-w WORDS RET'
  217.      Search for WORDS, ignoring differences in punctuation.
  218. `C-r RET C-w WORDS RET'
  219.      Search backward for WORDS, ignoring differences in punctuation.
  220.    Word search is a special case of non-incremental search.  It is
  221. invoked with `C-s RET C-w' followed by the search string, which must
  222. always be terminated with another RET.  Being non-incremental, this
  223. search does not start until the argument is terminated.  It works by
  224. constructing a regular expression and searching for that.  *Note Regexp
  225. Search::.
  226.    You can do a backward word search with `C-r RET C-w'.
  227.    Forward and backward word searches are implemented by the commands
  228. `word-search-forward' and `word-search-backward'.  You can bind these
  229. commands to keys.  The reason that incremental search is programmed to
  230. invoke them as well is that `C-s RET C-w' is the traditional Emacs
  231. sequence of keys for word search.
  232. File: lemacs,  Node: Regexp Search,  Next: Regexps,  Prev: Word Search,  Up: Search
  233. Regular Expression Search
  234. =========================
  235.    A "regular expression" ("regexp", for short) is a pattern that
  236. denotes a set of strings, possibly an infinite set.  Searching for
  237. matches for a regexp is a powerful operation that editors on Unix
  238. systems have traditionally offered.  In GNU Emacs, you can search for
  239. the next match for a regexp either incrementally or not.
  240.    Incremental search for a regexp is done by typing `C-M-s'
  241. (`isearch-forward-regexp').  This command reads a search string
  242. incrementally just like `C-s', but it treats the search string as a
  243. regexp rather than looking for an exact match against the text in the
  244. buffer.  Each time you add text to the search string, you make the
  245. regexp longer, and the new regexp is searched for.  A reverse regexp
  246. search command `isearch-backward-regexp' also exists but no key runs it.
  247.    All of the control characters that do special things within an
  248. ordinary incremental search have the same functionality in incremental
  249. regexp search.  Typing `C-s' or `C-r' immediately after starting a
  250. search retrieves the last incremental search regexp used: incremental
  251. regexp and non-regexp searches have independent defaults.
  252.    Non-Incremental search for a regexp is done by the functions
  253. `re-search-forward' and `re-search-backward'.  You can invoke them with
  254. `M-x' or bind them to keys.  You can also call `re-search-forward' by
  255. way of incremental regexp search with `C-M-s RET'.
  256. File: lemacs,  Node: Regexps,  Next: Search Case,  Prev: Regexp Search,  Up: Search
  257. Syntax of Regular Expressions
  258. =============================
  259.    Regular expressions have a syntax in which a few characters are
  260. special constructs and the rest are "ordinary".  An ordinary character
  261. is a simple regular expression which matches that character and nothing
  262. else.  The special characters are `$', `^', `.', `*', `+', `?', `[',
  263. `]' and `\'; no new special characters will be defined.  Any other
  264. character appearing in a regular expression is ordinary, unless a `\'
  265. precedes it.
  266.    For example, `f' is not a special character, so it is ordinary, and
  267. therefore `f' is a regular expression that matches the string `f' and
  268. no other string.  (It does not match the string `ff'.)  Likewise, `o'
  269. is a regular expression that matches only `o'.
  270.    Any two regular expressions A and B can be concatenated.  The result
  271. is a regular expression which matches a string if A matches some amount
  272. of the beginning of that string and B matches the rest of the string.
  273.    As a simple example, you can concatenate the regular expressions `f'
  274. and `o' to get the regular expression `fo', which matches only the
  275. string `fo'.  To do something nontrivial, you need to use one of the
  276. following special characters:
  277. `. (Period)'
  278.      is a special character that matches any single character except a
  279.      newline.  Using concatenation, you can make regular expressions
  280.      like `a.b' which matches any three-character string which begins
  281.      with `a' and ends with `b'.
  282.      is not a construct by itself; it is a suffix, which means the
  283.      preceding regular expression is to be repeated as many times as
  284.      possible.  In `fo*', the `*' applies to the `o', so `fo*' matches
  285.      one `f' followed by any number of `o's.  The case of zero `o's is
  286.      allowed: `fo*' does match `f'.
  287.      `*' always applies to the smallest possible preceding expression.
  288.      Thus, `fo*' has a repeating `o', not a repeating `fo'.
  289.      The matcher processes a `*' construct by matching, immediately, as
  290.      many repetitions as it can find.  Then it continues with the rest
  291.      of the pattern.  If that fails, backtracking occurs, discarding
  292.      some of the matches of the `*'-modified construct in case that
  293.      makes it possible to match the rest of the pattern.  For example,
  294.      matching `ca*ar' against the string `caaar', the `a*' first tries
  295.      to match all three `a's; but the rest of the pattern is `ar' and
  296.      there is only `r' left to match, so this try fails.  The next
  297.      alternative is for `a*' to match only two `a's.  With this choice,
  298.      the rest of the regexp matches successfully.
  299.      Is a suffix character similar to `*' except that it requires that
  300.      the preceding expression be matched at least once.  For example,
  301.      `ca+r' will match the strings `car' and `caaaar' but not the
  302.      string `cr', whereas `ca*r' would match all three strings.
  303.      Is a suffix character similar to `*' except that it can match the
  304.      preceding expression either once or not at all.  For example,
  305.      `ca?r' will match `car' or `cr'; nothing else.
  306. `[ ... ]'
  307.      `[' begins a "character set", which is terminated by a `]'.  In
  308.      the simplest case, the characters between the two form the set.
  309.      Thus, `[ad]' matches either one `a' or one `d', and `[ad]*'
  310.      matches any string composed of just `a's and `d's (including the
  311.      empty string), from which it follows that `c[ad]*r' matches `cr',
  312.      `car', `cdr', `caddaar', etc.
  313.      You can include character ranges in a character set by writing two
  314.      characters with a `-' between them.  Thus, `[a-z]' matches any
  315.      lower-case letter.  Ranges may be intermixed freely with
  316.      individual characters, as in `[a-z$%.]', which matches any lower
  317.      case letter or `$', `%' or period.
  318.      Note that inside a character set the usual special characters are
  319.      not special any more.  A completely different set of special
  320.      characters exists inside character sets: `]', `-' and `^'.
  321.      To include a `]' in a character set, you must make it the first
  322.      character.  For example, `[]a]' matches `]' or `a'.  To include a
  323.      `-', write `---', which is a range containing only `-'.  To
  324.      include `^', make it other than the first character in the set.
  325. `[^ ... ]'
  326.      `[^' begins a "complement character set", which matches any
  327.      character except the ones specified.  Thus, `[^a-z0-9A-Z]' matches
  328.      all characters except letters and digits.
  329.      `^' is not special in a character set unless it is the first
  330.      character.  The character following the `^' is treated as if it
  331.      were first (`-' and `]' are not special there).
  332.      Note that a complement character set can match a newline, unless
  333.      newline is mentioned as one of the characters not to match.
  334.      is a special character that matches the empty string, but only if
  335.      at the beginning of a line in the text being matched.  Otherwise,
  336.      it fails to match anything.  Thus, `^foo' matches a `foo' that
  337.      occurs at the beginning of a line.
  338.      is similar to `^' but matches only at the end of a line.  Thus,
  339.      `xx*$' matches a string of one `x' or more at the end of a line.
  340.      does two things: it quotes the special characters (including `\'),
  341.      and it introduces additional special constructs.
  342.      Because `\' quotes special characters, `\$' is a regular
  343.      expression that matches only `$', and `\[' is a regular expression
  344.      that matches only `[', and so on.
  345.    Note: for historical compatibility, special characters are treated as
  346. ordinary ones if they are in contexts where their special meanings make
  347. no sense.  For example, `*foo' treats `*' as ordinary since there is no
  348. preceding expression on which the `*' can act.  It is poor practice to
  349. depend on this behavior; better to quote the special character anyway,
  350. regardless of where is appears.
  351.    Usually, `\' followed by any character matches only that character.
  352. However, there are several exceptions: characters which, when preceded
  353. by `\', are special constructs.  Such characters are always ordinary
  354. when encountered on their own.  Here is a table of `\' constructs.
  355.      specifies an alternative.  Two regular expressions A and B with
  356.      `\|' in between form an expression that matches anything A or B
  357.      matches.
  358.      Thus, `foo\|bar' matches either `foo' or `bar' but no other string.
  359.      `\|' applies to the largest possible surrounding expressions.
  360.      Only a surrounding `\( ... \)' grouping can limit the grouping
  361.      power of `\|'.
  362.      Full backtracking capability exists to handle multiple uses of
  363.      `\|'.
  364. `\( ... \)'
  365.      is a grouping construct that serves three purposes:
  366.        1. To enclose a set of `\|' alternatives for other operations.
  367.           Thus, `\(foo\|bar\)x' matches either `foox' or `barx'.
  368.        2. To enclose a complicated expression for the postfix `*' to
  369.           operate on.  Thus, `ba\(na\)*' matches `bananana', etc., with
  370.           any (zero or more) number of `na' strings.
  371.        3. To mark a matched substring for future reference.
  372.      This last application is not a consequence of the idea of a
  373.      parenthetical grouping; it is a separate feature which happens to
  374.      be assigned as a second meaning to the same `\( ... \)' construct
  375.      because in practice there is no conflict between the two meanings.
  376.      Here is an explanation:
  377. `\DIGIT'
  378.      after the end of a `\( ... \)' construct, the matcher remembers the
  379.      beginning and end of the text matched by that construct.  Then,
  380.      later on in the regular expression, you can use `\' followed by
  381.      DIGIT to mean "match the same text matched the DIGIT'th time by the
  382.      `\( ... \)' construct."
  383.      The strings matching the first nine `\( ... \)' constructs
  384.      appearing in a regular expression are assigned numbers 1 through 9
  385.      in order that the open-parentheses appear in the regular
  386.      expression.  `\1' through `\9' may be used to refer to the text
  387.      matched by the corresponding `\( ... \)' construct.
  388.      For example, `\(.*\)\1' matches any newline-free string that is
  389.      composed of two identical halves.  The `\(.*\)' matches the first
  390.      half, which may be anything, but the `\1' that follows must match
  391.      the same exact text.
  392.      matches the empty string, provided it is at the beginning of the
  393.      buffer.
  394.      matches the empty string, provided it is at the end of the buffer.
  395.      matches the empty string, provided it is at the beginning or end
  396.      of a word.  Thus, `\bfoo\b' matches any occurrence of `foo' as a
  397.      separate word.  `\bballs?\b' matches `ball' or `balls' as a
  398.      separate word.
  399.      matches the empty string, provided it is not at the beginning or
  400.      end of a word.
  401.      matches the empty string, provided it is at the beginning of a
  402.      word.
  403.      matches the empty string, provided it is at the end of a word.
  404.      matches any word-constituent character.  The editor syntax table
  405.      determines which characters these are.
  406.      matches any character that is not a word-constituent.
  407. `\sCODE'
  408.      matches any character whose syntax is CODE.  CODE is a character
  409.      which represents a syntax code: thus, `w' for word constituent,
  410.      `-' for whitespace, `(' for open-parenthesis, etc.  *Note Syntax::.
  411. `\SCODE'
  412.      matches any character whose syntax is not CODE.
  413.    Here is a complicated regexp, used by Emacs to recognize the end of a
  414. sentence together with any whitespace that follows.  It is given in Lisp
  415. syntax to enable you to distinguish the spaces from the tab characters.
  416. In Lisp syntax, the string constant begins and ends with a
  417. double-quote.  `\"' stands for a double-quote as part of the regexp,
  418. `\\' for a backslash as part of the regexp, `\t' for a tab and `\n' for
  419. a newline.
  420.      "[.?!][]\"')]*\\($\\|\t\\|  \\)[ \t\n]*"
  421. This regexp contains four parts: a character set matching period, `?'
  422. or `!'; a character set matching close-brackets, quotes or parentheses,
  423. repeated any number of times; an alternative in backslash-parentheses
  424. that matches end-of-line, a tab or two spaces; and a character set
  425. matching whitespace characters, repeated any number of times.
  426. File: lemacs,  Node: Search Case,  Next: Replace,  Prev: Regexps,  Up: Search
  427. Searching and Case
  428. ==================
  429.    All searches in Emacs normally ignore the case of the text they are
  430. searching through; if you specify searching for `FOO', `Foo' and `foo'
  431. are also considered a match.  Regexps, and in particular character
  432. sets, are included: `[aB]' matches `a' or `A' or `b' or `B'.
  433.    If you want a case-sensitive search, set the variable
  434. `case-fold-search' to `nil'.  Then all letters must match exactly,
  435. including case. `case-fold-search' is a per-buffer variable; altering
  436. it affects only the current buffer, but there is a default value which
  437. you can change as well.  *Note Locals::.
  438. File: lemacs,  Node: Replace,  Next: Other Repeating Search,  Prev: Search Case,  Up: Search
  439. Replacement Commands
  440. ====================
  441.    Global search-and-replace operations are not needed as often in
  442. Emacs as they are in other editors, but they are available.  In
  443. addition to the simple `replace-string' command which is like that
  444. found in most editors, there is a `query-replace' command which asks
  445. you, for each occurrence of a pattern, whether to replace it.
  446.    The replace commands all replace one string (or regexp) with one
  447. replacement string.  It is possible to perform several replacements in
  448. parallel using the command `expand-region-abbrevs'.  *Note Expanding
  449. Abbrevs::.
  450. * Menu:
  451. * Unconditional Replace::  Replacing all matches for a string.
  452. * Regexp Replace::         Replacing all matches for a regexp.
  453. * Replacement and Case::   How replacements preserve case of letters.
  454. * Query Replace::          How to use querying.
  455. File: lemacs,  Node: Unconditional Replace,  Next: Regexp Replace,  Prev: Replace,  Up: Replace
  456. Unconditional Replacement
  457. -------------------------
  458. `M-x replace-string RET STRING RET NEWSTRING RET'
  459.      Replace every occurrence of STRING with NEWSTRING.
  460. `M-x replace-regexp RET REGEXP RET NEWSTRING RET'
  461.      Replace every match for REGEXP with NEWSTRING.
  462.    To replace every instance of `foo' after point with `bar', use the
  463. command `M-x replace-string' with the two arguments `foo' and `bar'.
  464. Replacement occurs only after point: if you want to cover the whole
  465. buffer you must go to the beginning first.  By default, all occurrences
  466. up to the end of the buffer are replaced.  To limit replacement to part
  467. of the buffer, narrow to that part of the buffer before doing the
  468. replacement (*note Narrowing::.).
  469.    When `replace-string' exits, point is left at the last occurrence
  470. replaced.  The value of point when the `replace-string' command was
  471. issued is remembered on the mark ring; `C-u C-SPC' moves back there.
  472.    A numeric argument restricts replacement to matches that are
  473. surrounded by word boundaries.
  474. File: lemacs,  Node: Regexp Replace,  Next: Replacement and Case,  Prev: Unconditional Replace,  Up: Replace
  475. Regexp Replacement
  476. ------------------
  477.    `replace-string' replaces exact matches for a single string.  The
  478. similar command `replace-regexp' replaces any match for a specified
  479. pattern.
  480.    In `replace-regexp', the NEWSTRING need not be constant.  It can
  481. refer to all or part of what is matched by the REGEXP.  `\&' in
  482. NEWSTRING stands for the entire text being replaced.  `\D' in
  483. NEWSTRING, where D is a digit, stands for whatever matched the D'th
  484. parenthesized grouping in REGEXP.  For example,
  485.      M-x replace-regexp RET c[ad]+r RET \&-safe RET
  486. would replace (for example) `cadr' with `cadr-safe' and `cddr' with
  487. `cddr-safe'.
  488.      M-x replace-regexp RET \(c[ad]+r\)-safe RET \1 RET
  489. would perform exactly the opposite replacements.  To include a `\' in
  490. the text to replace with, you must give `\\'.
  491. File: lemacs,  Node: Replacement and Case,  Next: Query Replace,  Prev: Regexp Replace,  Up: Replace
  492. Replace Commands and Case
  493. -------------------------
  494.    If the arguments to a replace command are in lower case, the command
  495. preserves case when it makes a replacement.  Thus, the command
  496.      M-x replace-string RET foo RET bar RET
  497. replaces a lower case `foo' with a lower case `bar', `FOO' with `BAR',
  498. and `Foo' with `Bar'.  If upper case letters are used in the second
  499. argument, they remain upper case every time that argument is inserted.
  500. If upper case letters are used in the first argument, the second
  501. argument is always substituted exactly as given, with no case
  502. conversion.  Likewise, if the variable `case-replace' is set to `nil',
  503. replacement is done without case conversion.  If `case-fold-search' is
  504. set to `nil', case is significant in matching occurrences of `foo' to
  505. replace; also, case conversion of the replacement string is not done.
  506. File: lemacs,  Node: Query Replace,  Prev: Replacement and Case,  Up: Replace
  507. Query Replace
  508. -------------
  509. `M-% STRING RET NEWSTRING RET'
  510. `M-x query-replace RET STRING RET NEWSTRING RET'
  511.      Replace some occurrences of STRING with NEWSTRING.
  512. `M-x query-replace-regexp RET REGEXP RET NEWSTRING RET'
  513.      Replace some matches for REGEXP with NEWSTRING.
  514.    If you want to change only some of the occurrences of `foo' to
  515. `bar', not all of them, you can (`query-replace') instead of `M-%'.
  516. This command finds occurrences of `foo' one by one, displays each
  517. occurrence, and asks you whether to replace it.  A numeric argument to
  518. `query-replace' tells it to consider only occurrences that are bounded
  519. by word-delimiter characters.
  520.    Aside from querying, `query-replace' works just like
  521. `replace-string', and `query-replace-regexp' works just like
  522. `replace-regexp'.
  523.    The things you can type when you are shown an occurrence of STRING
  524. or a match for REGEXP are:
  525. `SPC'
  526.      to replace the occurrence with NEWSTRING.  This preserves case,
  527.      just like `replace-string', provided `case-replace' is non-`nil',
  528.      as it normally is.
  529. `DEL'
  530.      to skip to the next occurrence without replacing this one.
  531. `, (Comma)'
  532.      to replace this occurrence and display the result.  You are then
  533.      prompted for another input character, however, since the
  534.      replacement has already been made, DEL and SPC are equivalent.  At
  535.      this point, you can type `C-r' (see below) to alter the replaced
  536.      text.  To undo the replacement, you can type `C-x u'.  This exits
  537.      the `query-replace'.  If you want to do further replacement you
  538.      must use `C-x ESC' to restart (*note Repetition::.).
  539. `ESC'
  540.      to exit without doing any more replacements.
  541. `. (Period)'
  542.      to replace this occurrence and then exit.
  543.      to replace all remaining occurrences without asking again.
  544.      to go back to the location of the previous occurrence (or what
  545.      used to be an occurrence), in case you changed it by mistake.
  546.      This works by popping the mark ring.  Only one `^' in a row is
  547.      allowed, because only one previous replacement location is kept
  548.      during `query-replace'.
  549. `C-r'
  550.      to enter a recursive editing level, in case the occurrence needs
  551.      to be edited rather than just replaced with NEWSTRING.  When you
  552.      are done, exit the recursive editing level with `C-M-c' and the
  553.      next occurrence will be displayed.  *Note Recursive Edit::.
  554. `C-w'
  555.      to delete the occurrence, and then enter a recursive editing level
  556.      as in `C-r'.  Use the recursive edit to insert text to replace the
  557.      deleted occurrence of STRING.  When done, exit the recursive
  558.      editing level with `C-M-c' and the next occurrence will be
  559.      displayed.
  560. `C-l'
  561.      to redisplay the screen and then give another answer.
  562. `C-h'
  563.      to display a message summarizing these options, then give another
  564.      answer.
  565.    If you type any other character, Emacs exits the `query-replace', and
  566. executes the character as a command.  To restart the `query-replace',
  567. use `C-x ESC', which repeats the `query-replace' because it used the
  568. minibuffer to read its arguments.  *Note C-x ESC: Repetition.
  569. File: lemacs,  Node: Other Repeating Search,  Prev: Replace,  Up: Search
  570. Other Search-and-Loop Commands
  571. ==============================
  572.    Here are some other commands that find matches for a regular
  573. expression.  They all operate from point to the end of the buffer.
  574. `M-x occur'
  575.      Print each line that follows point and contains a match for the
  576.      specified regexp.  A numeric argument specifies the number of
  577.      context lines to print before and after each matching line; the
  578.      default is none.
  579.      The buffer `*Occur*' containing the output serves as a menu for
  580.      finding occurrences in their original context.  Find an occurrence
  581.      as listed in `*Occur*', position point there, and type `C-c C-c';
  582.      this switches to the buffer that was searched and moves point to
  583.      the original of the same occurrence.
  584. `M-x list-matching-lines'
  585.      Synonym for `M-x occur'.
  586. `M-x count-matches'
  587.      Print the number of matches following point for the specified
  588.      regexp.
  589. `M-x delete-non-matching-lines'
  590.      Delete each line that follows point and does not contain a match
  591.      for the specified regexp.
  592. `M-x delete-matching-lines'
  593.      Delete each line that follows point and contains a match for the
  594.      specified regexp.
  595. File: lemacs,  Node: Fixit,  Next: Files,  Prev: Search,  Up: Top
  596. Commands for Fixing Typos
  597. *************************
  598.    This chapter describes commands that are especially useful when you
  599. catch a mistake in your text just after you have made it, or change your
  600. mind while composing text on line.
  601. * Menu:
  602. * Kill Errors:: Commands to kill a batch of recently entered text.
  603. * Transpose::   Exchanging two characters, words, lines, lists...
  604. * Fixing Case:: Correcting case of last word entered.
  605. * Spelling::    Apply spelling checker to a word, or a whole file.
  606. File: lemacs,  Node: Kill Errors,  Next: Transpose,  Prev: Fixit,  Up: Fixit
  607. Killing Your Mistakes
  608. =====================
  609. `DEL'
  610.      Delete last character (`delete-backward-char').
  611. `M-DEL'
  612.      Kill last word (`backward-kill-word').
  613. `C-x DEL'
  614.      Kill to beginning of sentence (`backward-kill-sentence').
  615.    The DEL character (`delete-backward-char') is the most important
  616. correction command.  When used among graphic (self-inserting)
  617. characters, it can be thought of as canceling the last character typed.
  618.    When your mistake is longer than a couple of characters, it might be
  619. more convenient to use `M-DEL' or `C-x DEL'.  `M-DEL' kills back to the
  620. start of the last word, and `C-x DEL' kills back to the start of the
  621. last sentence.  `C-x DEL' is particularly useful when you are thinking
  622. of what to write as you type it, in case you change your mind about
  623. phrasing.  `M-DEL' and `C-x DEL' save the killed text for `C-y' and
  624. `M-y' to retrieve.  *Note Yanking::.
  625.    `M-DEL' is often useful even when you have typed only a few
  626. characters wrong, if you know you are confused in your typing and aren't
  627. sure exactly what you typed.  At such a time, you cannot correct with
  628. DEL except by looking at the screen to see what you did.  It requires
  629. less thought to kill the whole word and start over.
  630. File: lemacs,  Node: Transpose,  Next: Fixing Case,  Prev: Kill Errors,  Up: Fixit
  631. Transposing Text
  632. ================
  633. `C-t'
  634.      Transpose two characters (`transpose-chars').
  635. `M-t'
  636.      Transpose two words (`transpose-words').
  637. `C-M-t'
  638.      Transpose two balanced expressions (`transpose-sexps').
  639. `C-x C-t'
  640.      Transpose two lines (`transpose-lines').
  641.    The common error of transposing two adjacent characters can be fixed
  642. with the `C-t' command (`transpose-chars').  Normally, `C-t' transposes
  643. the two characters on either side of point.  When given at the end of a
  644. line, `C-t' transposes the last two characters on the line, rather than
  645. transposing the last character of the line with the newline, which
  646. would be useless.  If you catch a transposition error right away, you
  647. can fix it with just `C-t'.  If you catch the error later,  move the
  648. cursor back to between the two transposed characters.  If you
  649. transposed a space with the last character of the word before it, the
  650. word motion commands are a good way of getting there.  Otherwise, a
  651. reverse search (`C-r') is often the best way.  *Note Search::.
  652.    `Meta-t' (`transpose-words') transposes the word before point with
  653. the word after point.  It moves point forward over a word, dragging the
  654. word preceding or containing point forward as well.  The punctuation
  655. characters between the words do not move.  For example, `FOO, BAR'
  656. transposes into `BAR, FOO' rather than `BAR FOO,'.
  657.    `C-M-t' (`transpose-sexps') is a similar command for transposing two
  658. expressions (*note Lists::.), and `C-x C-t' (`transpose-lines')
  659. exchanges lines.  It works like `M-t' but in determins the division of
  660. the text into syntactic units differently.
  661.    A numeric argument to a transpose command serves as a repeat count:
  662. it tells the transpose command to move the character (word, sexp, line)
  663. before or containing point across several other characters (words,
  664. sexps, lines).  For example, `C-u 3 C-t' moves the character before
  665. point forward across three other characters.  This is equivalent to
  666. repeating `C-t' three times.  `C-u - 4 M-t' moves the word before point
  667. backward across four words.  `C-u - C-M-t' would cancel the effect of
  668. plain `C-M-t'.
  669.    A numeric argument of zero transposes the character (word, sexp,
  670. line) ending after point with the one ending after the mark (otherwise a
  671. command with a repeat count of zero would do nothing).
  672. File: lemacs,  Node: Fixing Case,  Next: Spelling,  Prev: Transpose,  Up: Fixit
  673. Case Conversion
  674. ===============
  675. `M-- M-l'
  676.      Convert last word to lower case.  Note `Meta--' is Meta-minus.
  677. `M-- M-u'
  678.      Convert last word to all upper case.
  679. `M-- M-c'
  680.      Convert last word to lower case with capital initial.
  681.    A  common error is to type words in the wrong case.  Because of this,
  682. the word case-conversion commands `M-l', `M-u' and `M-c' do not move
  683. the cursor when used with a negative argument.  As soon as you see you
  684. have mistyped the last word, you can simply case-convert it and
  685. continue typing.  *Note Case::.
  686. File: lemacs,  Node: Spelling,  Prev: Fixing Case,  Up: Fixit
  687. Checking and Correcting Spelling
  688. ================================
  689. `M-$'
  690.      Check and correct spelling of word (`spell-word').
  691. `M-x spell-buffer'
  692.      Check and correct spelling of each word in the buffer.
  693. `M-x spell-region'
  694.      Check and correct spelling of each word in the region.
  695. `M-x spell-string'
  696.      Check spelling of specified word.
  697.    To check the spelling of the word before point, and optionally
  698. correct it, use the command `M-$' (`spell-word').  This command runs an
  699. inferior process containing the `spell' program to see whether the word
  700. is correct English.  If it is not, it asks you to edit the word (in the
  701. minibuffer) into a corrected spelling, and then performs a
  702. `query-replace' to substitute the corrected spelling for the old one
  703. throughout the buffer.
  704.    If you exit the minibuffer without altering the original spelling, it
  705. means you do not want to do anything to that word.  In that case, the
  706. `query-replace' is not done.
  707.    `M-x spell-buffer' checks each word in the buffer the same way that
  708. `spell-word' does, doing a `query-replace' for every incorrect word if
  709. appropriate.
  710.    `M-x spell-region' is similar to `spell-buffer' but operates only on
  711. the region, not the entire buffer.
  712.    `M-x spell-string' reads a string as an argument and checks whether
  713. that is a correctly spelled English word.  It prints a message giving
  714. the answer in the echo area.
  715. File: lemacs,  Node: Files,  Next: Buffers,  Prev: Fixit,  Up: Top
  716. File Handling
  717. *************
  718.    The basic unit of stored data in Unix is the "file".  To edit a file,
  719. you must tell Emacs to examine the file and prepare a buffer containing
  720. a copy of the file's text.  This is called "visiting" the file.  Editing
  721. commands apply directly to text in the buffer; that is, to the copy
  722. inside Emacs.  Your changes appear in the file itself only when you
  723. "save" the buffer back into the file.
  724.    In addition to visiting and saving files, Emacs can delete, copy,
  725. rename, and append to files, and operate on file directories.
  726. * Menu:
  727. * File Names::   How to type and edit file name arguments.
  728. * Visiting::     Visiting a file prepares Emacs to edit the file.
  729. * Saving::       Saving makes your changes permanent.
  730. * Reverting::    Reverting cancels all the changes not saved.
  731. * Auto Save::    Auto Save periodically protects against loss of data.
  732. * ListDir::      Listing the contents of a file directory.
  733. * Dired::        "Editing" a directory to delete, rename, etc.
  734.                   the files in it.
  735. * Misc File Ops:: Other things you can do on files.
  736. File: lemacs,  Node: File Names,  Next: Visiting,  Prev: Files,  Up: Files
  737. File Names
  738. ==========
  739.    Most Emacs commands that operate on a file require you to specify the
  740. file name.  (Saving and reverting are exceptions; the buffer knows which
  741. file name to use for them.)  File names are specified in the minibuffer
  742. (*note Minibuffer::.).  "Completion" is available, to make it easier to
  743. specify long file names.  *Note Completion::.
  744.    There is always a "default file name" which is used if you enter an
  745. empty argument by typing just RET.  Normally the default file name is
  746. the name of the file visited in the current buffer; this makes it easy
  747. to operate on that file with any of the Emacs file commands.
  748.    Each buffer has a default directory, normally the same as the
  749. directory of the file visited in that buffer.  When Emacs reads a file
  750. name, the default directory is used if you do not specify a directory.
  751. If you specify a directory in a relative fashion, with a name that does
  752. not start with a slash, it is interpreted with respect to the default
  753. directory.  The default directory of the current buffer is kept in the
  754. variable `default-directory', which has a separate value in every
  755. buffer.  The value of the variable should end with a slash.
  756.    For example, if the default file name is `/u/rms/gnu/gnu.tasks' then
  757. the default directory is `/u/rms/gnu/'.  If you type just `foo', which
  758. does not specify a directory, it is short for `/u/rms/gnu/foo'.
  759. `../.login' would stand for `/u/rms/.login'.  `new/foo' would stand for
  760. the filename `/u/rms/gnu/new/foo'.
  761.    The variable `default-directory-alist' takes an alist of major modes
  762. and their opinions on `default-directory' as a Lisp expression to
  763. evaluate.  A resulting value of `nil' is ignored in favor of
  764. `default-directory'.
  765.    You can create a new directory with the function `make-directory',
  766. which takes as an argument a file name string. The current directory is
  767. displayed in the minibuffer when the function is called; you can delete
  768. the old directory name and supply a new directory name. For example, if
  769. the current directory is `/u/rms/gnu', you can delete `gnu' and type
  770. `oryx' and RET to create `/u/rms/oryx'.  Removing a directory is
  771. similar to creating one.  To remove a directory, use
  772. `remove-directory'; it takes one argument a file name string.
  773.    The command `M-x pwd' prints the current buffer's default directory,
  774. and the command `M-x cd' sets it (to a value read using the
  775. minibuffer).  A buffer's default directory changes only when the `cd'
  776. command is used.  A file-visiting buffer's default directory is
  777. initialized to the directory of the file that is visited there.  If a
  778. buffer is created with `C-x b', its default directory is copied from
  779. that of the buffer that was current at the time.
  780.    The default directory name actually appears in the minibuffer when
  781. the minibuffer becomes active to read a file name.  This serves two
  782. purposes: it shows you what the default is, so that you can type a
  783. relative file name and know with certainty what it will mean, and it
  784. allows you to edit the default to specify a different directory.  To
  785. inhibit the insertion of the default directory, set the variable
  786. `insert-default-directory' to `nil'.
  787.    Note that it is legitimate to type an absolute file name after you
  788. enter the minibuffer, ignoring the presence of the default directory
  789. name.  The final minibuffer contents may look invalid, but that is not
  790. so.  *Note Minibuffer File::.
  791.    `$' in a file name is used to substitute environment variables.  For
  792. example, if you have used the shell command `setenv FOO rms/hacks' to
  793. set up an environment variable named `FOO', then you can use
  794. `/u/$FOO/test.c' or `/u/${FOO}/test.c' as an abbreviation for
  795. `/u/rms/hacks/test.c'.  The environment variable name consists of all
  796. the alphanumeric characters after the `$'; alternatively, it may be
  797. enclosed in braces after the `$'.  Note that the `setenv' command
  798. affects Emacs only if done before Emacs is started.
  799.    To access a file with `$' in its name, type `$$'.  This pair is
  800. converted to a single `$' at the same time variable substitution is
  801. performed for single `$'.  The Lisp function that performs the
  802. substitution is called `substitute-in-file-name'.  The substitution is
  803. performed only on filenames read as such using the minibuffer.
  804.